package com.shangbang.verb.service.impl.recruit;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shangbang.verb.dao.recruit.MiddleMapper;
import com.shangbang.verb.dao.recruit.UserMapper;
import com.shangbang.verb.dao.recruit.WorkMapper;
import com.shangbang.verb.entity.recruit.MiddleEntity;
import com.shangbang.verb.entity.recruit.UserEntity;
import com.shangbang.verb.entity.recruit.WorkEntity;
import com.shangbang.verb.service.recruit.MiddleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class MiddleServiceImpl implements MiddleService {
    @Autowired
    private MiddleMapper middleMapper;
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void deliverWork(Integer uid, Integer wordkId, Integer publishId) {
        MiddleEntity middleEntity = new MiddleEntity();
        middleEntity.setUid(uid);
        middleEntity.setWorkId(wordkId);
        middleEntity.setPublishId(publishId);
        middleEntity.setCreateTime(new Date());
        middleMapper.insert(middleEntity);
    }

    @Override
    public Object partList(Integer publishId) {
        List<MiddleEntity>  list = middleMapper.getListByPublishId(publishId);
        JSONArray jsonArray = new JSONArray();

        for (int i = 0; i <list.size() ; i++) {
            UserEntity userEntity = userMapper.selectById(list.get(i).getUid());
            if(userEntity!=null){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id",list.get(i).getId());
                jsonObject.put("uid",list.get(i).getUid());
                jsonObject.put("workId",list.get(i).getWorkId());
                jsonObject.put("flag",list.get(i).getFlag());
                jsonObject.put("publishId",list.get(i).getPublishId());
                jsonObject.put("createTime",list.get(i).getCreateTime());
                jsonObject.put("executionTime",list.get(i).getExecutionTime());
                jsonObject.put("phone",userEntity.getPhone());
                jsonObject.put("gender",userEntity.getGender());
                jsonObject.put("icon",userEntity.getIcon());
                jsonObject.put("aboutMe",userEntity.getAboutMe());
                jsonObject.put("wxuid",userEntity.getWxuid());
                jsonObject.put("nickname",userEntity.getNickname());
                jsonArray.add(jsonObject);

            }
        }
            return  jsonArray;

    }

    @Override
    public void agreeDelivery(Integer middleId, String time, Integer flag)throws ParseException {
        MiddleEntity middleEntity = new MiddleEntity();
        middleEntity =  middleMapper.selectById(middleId);
        if(middleEntity!=null){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            middleEntity.setFlag(flag);
            middleEntity.setExecutionTime(formatter.parse(time));
            middleMapper.updateById(middleEntity);
        }
    }

    @Override
    public Object myDelivery(Integer uid, Integer page, Integer num,Integer type)throws ParseException {
        List<MiddleEntity>  list = null;
        if(type==0){
              list = middleMapper.getListByUid(uid,page,num);
        }else if (type==1){
            Date nowTime = new Date();
              list =  middleMapper.myDeliveryTime(nowTime,uid,page,num);
        }
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            WorkEntity workEntity =workMapper.selectById(list.get(i).getWorkId());
            if (workEntity!=null){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id",list.get(i).getId());
                jsonObject.put("uid",list.get(i).getUid());
                jsonObject.put("workId",list.get(i).getWorkId());
                jsonObject.put("flag",list.get(i).getFlag());
                jsonObject.put("publishId",list.get(i).getPublishId());
                jsonObject.put("createTime",list.get(i).getCreateTime());
                jsonObject.put("executionTime",list.get(i).getExecutionTime());

                jsonObject.put("claim",workEntity.getClaim());
                jsonObject.put("enterpriseName",workEntity.getEnterpriseName());
                jsonObject.put("img",workEntity.getImg());
                jsonObject.put("introduction",workEntity.getIntroduction());
                jsonObject.put("lat",workEntity.getLat());
                jsonObject.put("lng",workEntity.getLng());
                jsonObject.put("phone",workEntity.getPhone());
                jsonObject.put("position",workEntity.getPosition());
                jsonObject.put("place",workEntity.getPlace());
                jsonObject.put("salary",workEntity.getSalary());
                jsonObject.put("welfare",workEntity.getWelfare());
                jsonObject.put("workType",workEntity.getWorkType());

                jsonArray.add(jsonObject);
            }

        }
        return jsonArray;
    }

}
